package TecMaster.Ui;

import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Font;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 * Classe responsavel pela tela de logon.
 * 
 * @author Flavio Ramalho
 * 
 */
public class MainLogon extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JLabel labelLogin, labelSenha;
	private JPanel panel;
	private JButton botaoLogin, botaoSair;
	private JTextField tfLogin;
	private JPasswordField pfSenha;

	private final String LOGIN = "admin";
	private final char[] SENHA = { 'a', 'd', 'm', 'i', 'n' };

	/**
	 * Construtor do main logon
	 */
	private MainLogon() {
		super("TecMaster - Logon");
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("/imagens/TM.png")));
		panel = new JPanel(null);
		setBounds(new java.awt.Rectangle(0, 0, 300, 300));
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setBackground(Color.LIGHT_GRAY);

		labelLogin = new JLabel("Login: ");
		panel.add(labelLogin);
		labelLogin.setFont(new Font("corbel", Font.PLAIN, 15));
		labelLogin.setBounds(50, 60, 50, 18);

		labelSenha = new JLabel("Senha: ");
		panel.add(labelSenha);
		labelSenha.setFont(new Font("corbel", Font.PLAIN, 15));
		labelSenha.setBounds(50, 110, 53, 15);

		tfLogin = new JTextField();
		panel.add(tfLogin);
		tfLogin.setBounds(100, 56, 130, 25);
		pfSenha = new JPasswordField();
		pfSenha.addActionListener(this);
		panel.add(pfSenha);
		pfSenha.setBounds(100, 106, 130, 25);

		botaoLogin = new JButton("Login");
		try {
			botaoLogin.setIcon(new ImageIcon(getClass().getResource(
					"/imagens/key_go.png")));
		} catch (Exception e) {
			System.out.println("Adicione a pasta imagens na pasta bin.");
			System.exit(0);
		}
		panel.add(botaoLogin);
		botaoLogin.setBounds(40, 185, 90, 30);
		botaoLogin.addActionListener(this);

		botaoSair = new JButton("Sair");
		botaoSair.setIcon(new ImageIcon(getClass().getResource(
				"/imagens/cancel.png")));
		panel.add(botaoSair);
		botaoSair.setBounds(160, 185, 90, 30);
		botaoSair.addActionListener(this);

		pack();
		setVisible(true);
		setSize(300, 300);
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		new MainLogon();
	}

	/**
	 * Retorna o login do usu�rio
	 * 
	 * @return o login do usu�rio
	 */
	public String getLOGIN() {
		return LOGIN;
	}

	/**
	 * retorna a senha do usu�rio
	 * 
	 * @return a senha do usu�rio
	 */
	public char[] getSENHA() {
		return SENHA;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == botaoSair)
			System.exit(0);
		if (e.getSource() == botaoLogin || e.getSource() == pfSenha) {
			char[] senhaEntrada = pfSenha.getPassword();
			if (getLOGIN().equals(tfLogin.getText())
					&& Arrays.equals(getSENHA(), senhaEntrada)) {
				JOptionPane.showMessageDialog(null, "Logado com sucesso!",
						"OK!", JOptionPane.INFORMATION_MESSAGE);
				try {
					new Opcoes();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				}
				setVisible(false);
			} else
				JOptionPane.showMessageDialog(null,
						"Login ou senha invalidos!", "ERRO!",
						JOptionPane.ERROR_MESSAGE);
			Arrays.fill(senhaEntrada, '0');
			pfSenha.selectAll();
		}
	}
}
